package com.example.football.controller;

import com.example.football.pojo.News;
import com.example.football.pojo.People;
import com.example.football.service.NewsService;
import com.example.football.service.PeopleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class NewsController {
    @Resource
    private NewsService cc;

    //全查
    @RequestMapping("newslist")
    public String queryAll(Model model) {
        List<News> list = cc.queryAll();
        //存入model
        model.addAttribute("list", list);
        //控制跳转

        return "newslist";
    }

    @RequestMapping("removeByNewsid")
    public String removeByNewsid(Integer id) {
        try {
            cc.removeByNewsid(id);
            return "redirect:/newslist";
        } catch (Exception e) {
            return "error";
        }
    }

    @PostMapping("findByTitle")
    public String findByTitle(String title, Model model){
        List<News> list = cc.queryByTitle(title);
        //存入model
        model.addAttribute("list", list);
        return "newslist";
    }



    @RequestMapping("addNewsPage")
    public String addNewsPage() {
        return "addnews";
    }

    @RequestMapping("addNews")
    public String addNews(News news) {
        try {
            cc.addNews(news);
            return "redirect:/newslist";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }



    @GetMapping("findById2")
    public String findById(Integer id, Model model){
        News list = cc.queryById(id);
        //存入model
        model.addAttribute("list", list);
        return "updatenews";
    }

    //数据修改
    @PostMapping("updateNews")
    public String changeNews(News news) {
//        try {
            cc.changeNews(news);
            return "redirect:/newslist";
//        } catch (Exception e) {
//            return "error";
//        }
    }
}
